﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ABMath.ModelFramework.Data;
using ABMath.ModelFramework.Visuals;
using ExtraControlLibrary.Forms;

namespace ExtraControlLibrary.VisualControls
{
    public partial class SpectralVisualControl : UserControl, IUpdateable
    {
        private SpectralVisual sv;

        public SpectralVisualControl(SpectralVisual sv)
        {
            InitializeComponent();
            this.sv = sv;
            sv.AssociatedControl = this;
            DataHasChanged();
        }

        public void DataHasChanged()
        {
            // re-display
            if (sv.Spectrum != null)
               xyPlotBox.SetMatrix(sv.Spectrum);
        }

        private void xyPlotBox_MouseClick(object sender, MouseEventArgs e)
        {
            if (sv == null)
                return;

            if (e.Button == MouseButtons.Right)
            {
                // select from a drop-down menu
                var options = new List<string>();
                options.Add("Copy Values to Clipboard");
                Point newLocation = PointToScreen(e.Location);
                var m = new DropMenuForm(newLocation, options);
                m.ShowDialog();
                {
                    switch (m.SelectedIndex)
                    {
                        case -1: // no selection
                            break;
                        case 0: // copy to clipboard
                            CopyToClipboard();
                            break;
                    }
                }
            }
        }

        private void CopyToClipboard()
        {
            if (sv.Spectrum==null)
                return;

            var sb = new StringBuilder(1024);
            for (int i = 0; i < sv.Spectrum.RowCount; ++i)
                sb.AppendLine(string.Format("{0:0.000}\t{1:0.000}", sv.Spectrum[i, 0], sv.Spectrum[i, 1]));

            Clipboard.SetText(sb.ToString());
        }
    }
}
